package sy6_Behavioral_Pattern.command.ex1;

import java.util.ArrayList;
import java.util.List;

/*
 * 智能家居遥控器RemoteController实现对所有不同家电（如灯、电视、电风扇等）的开关控制。
 * RemoteController聚合2个List集合：List<Command> onCommands和List<Command> offCommands，是对所有家电设备操作的抽象
 * 
 * 参考视频：https://www.bilibili.com/video/BV1G4411c7N4?p=102（韩顺平）
 */

class LightReceiver { //接收者：一种家电设备——灯
	public void on() {  
		System.out.println("开灯");
	}
	public void off() {  
		System.out.println("关灯");
	}
}

class TVReceiver { //接收者：一种家电设备——电视
	public void on() {  
		System.out.println("打开电视机");
	}
	public void off() {  
		System.out.println("关闭电视机");
	}
}

interface Command { //抽象命令
	public void  execute(); 
}

class LightOnCommand implements Command{  //具体命令：开灯
	private LightReceiver lightReceiver;  //聚合接收者

	public LightOnCommand(LightReceiver lightReceiver) {  //构造方法
		this.lightReceiver = lightReceiver;
	}
	
	@Override
	public void execute() {
		lightReceiver.on();
	}
}

class LightOffCommand implements Command{  //具体命令：关灯
	private LightReceiver lightReceiver;  //聚合接收者

	public LightOffCommand(LightReceiver lightReceiver) {
		this.lightReceiver = lightReceiver;
	}

	@Override
	public void execute() {
		lightReceiver.off();
	}
}

class TVOnCommand implements Command{  //具体命令：打开电视机
	private TVReceiver tvReceiver;  //聚合接收者

	public TVOnCommand(TVReceiver tvReceiver) {  //构造方法
		this.tvReceiver = tvReceiver;
	}
	
	@Override
	public void execute() {
		tvReceiver.on();
	}
}

class TVOffCommand implements Command{  //具体命令：关闭电视机
	private TVReceiver tvReceiver;  //聚合接收者

	public TVOffCommand(TVReceiver tvReceiver) {  //构造方法
		this.tvReceiver = tvReceiver;
	}
	
	@Override
	public void execute() {
		tvReceiver.off();
	}
}

class RemoteController { //调用者：可认为是遥控器
	//Invoker聚合Command
	private List<Command> onCommands=new ArrayList<Command>(); 
	private List<Command> offCommands=new ArrayList<Command>(); 
	
	public List<Command> getOnCommands() {
		return onCommands;
	}

	public List<Command> getOffCommands() {
		return offCommands;
	}

	public void onButtonWasPushed(int index) {  //定义请求方法
		onCommands.get(index).execute();  //调用命令者完成
	}
	
	public void offButtonWasPushed(int index) {  //定义请求方法
		offCommands.get(index).execute();  //调用命令者完成
	}
}


public class Client {  //客户端
	public static void main(String[] args) {
		RemoteController remoteController = new RemoteController(); //创建一个遥控器对象
		
		LightReceiver lightReceiver = new LightReceiver();  
		remoteController.getOnCommands().add(new LightOnCommand(lightReceiver));
		remoteController.getOffCommands().add(new LightOffCommand(lightReceiver));
		TVReceiver tvReceiver=new TVReceiver();
		remoteController.getOnCommands().add(new TVOnCommand(tvReceiver));
		remoteController.getOffCommands().add(new TVOffCommand(tvReceiver));
		
		System.out.println("回家了...");
		remoteController.onButtonWasPushed(0);	 //开灯
		remoteController.onButtonWasPushed(1);   //打开电视机
		System.out.println("准备出门了...");
		remoteController.offButtonWasPushed(1);	 //关闭电视机
		remoteController.offButtonWasPushed(0);  //关灯
	}
}
